package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ICompanyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公司Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-20
 */
@Service
public class CompanyServiceImpl implements ICompanyService
{
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private CompanyGoodsMapper companyGoodsMapper;
    @Autowired
    private CompanyCategroyMapper companyCategroyMapper;
    @Autowired
    private BaseCategroyMapper baseCategroyMapper;
    @Autowired
    private BaseGoodsMapper baseGoodsMapper;

    /**
     * 查询该公司的首页数据
     *
     * @param companyId
     * @return
     */
    @Override
    public AjaxResult getIndexData(long companyId) {
        String yesterday = IOUtil.getAfterTimeByDay(IOUtil.getdate("yyyy-MM-dd"),-1);
        Map<String,Object> map=companyMapper.getIndexData(companyId,yesterday);
        AjaxResult ajaxResult=AjaxResult.success(map);

        return ajaxResult;
    }

    /**
     * 同步商品
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult syncGoods(long id) {
        //删除原有商品分类+商品信息
        companyMapper.deleteCompanyCategory(id);
        companyMapper.deleteCompanyGoods(id);
        //获取基础商品分类，商品信息
        List<BaseCategroy> baseCategroys = baseCategroyMapper.selectBaseCategroyList(new BaseCategroy());
        List<BaseGoods> baseGoods = baseGoodsMapper.selectBaseGoodsList(new BaseGoods());
        List<CompanyCategroy> companyCategroys = new ArrayList<CompanyCategroy>();
        List<CompanyGoods> companyGoodsList = new ArrayList<CompanyGoods>();
        //处理公司商品分类
        for (BaseCategroy baseCategroy : baseCategroys) {
            CompanyCategroy companyCategroy = new CompanyCategroy();
            companyCategroy.setCompanyId(id);
            companyCategroy.setId(baseCategroy.getId()+"_"+id);//新ID=基础ID+公司ID，防止重复
            companyCategroy.setFid(baseCategroy.getFid()+"_"+id);
            companyCategroy.setName(baseCategroy.getName());
            companyCategroy.setSort(baseCategroy.getSort());
            companyCategroy.setLevel(baseCategroy.getLevel());
            companyCategroy.setRemark(baseCategroy.getRemark());
            companyCategroys.add(companyCategroy);
        }
        //处理公司商品信息
        for (BaseGoods goods : baseGoods) {
            CompanyGoods companyGoods = new CompanyGoods();
            companyGoods.setCompanyId(id);
            companyGoods.setCategroyId(goods.getCategroyId()+"_"+id);
            companyGoods.setName(goods.getName());
            companyGoods.setSort(goods.getSort());
            companyGoods.setPrice(goods.getPrice());
            companyGoods.setRemark(goods.getRemark());
            companyGoods.setCategroyName(goods.getCategroyName());
            companyGoods.setCode(goods.getCode());
            companyGoods.setUnit(goods.getUnit());
            companyGoods.setCunit(goods.getCunit());
            companyGoods.setImageUrl(goods.getImageUrl());
            companyGoods.setLife(goods.getLife());
            companyGoodsList.add(companyGoods);
        }
        //新增商品分类+商品信息
        int result = companyCategroyMapper.insertCompanyCategoryMulity(companyCategroys);
        if(result > 0){
            result = companyGoodsMapper.insertCompanyGoodsMulity(companyGoodsList);
        }

        return AjaxResult.success(result);
    }

    /**
     * 查询公司
     *
     * @param id 公司主键
     * @return 公司
     */
    @Override
    public Company selectCompanyById(Long id)
    {
        return companyMapper.selectCompanyById(id);
    }

    /**
     * 查询公司列表
     *
     * @param company 公司
     * @return 公司
     */
    @Override
    public List<Company> selectCompanyList(Company company)
    {
        return companyMapper.selectCompanyList(company);
    }

    /**
     * 新增公司
     *
     * @param company 公司
     * @return 结果
     */
    @Override
    public int insertCompany(Company company)
    {
        return companyMapper.insertCompany(company);
    }

    /**
     * 修改公司
     *
     * @param company 公司
     * @return 结果
     */
    @Override
    public int updateCompany(Company company)
    {
        return companyMapper.updateCompany(company);
    }

    /**
     * 批量删除公司
     *
     * @param ids 需要删除的公司主键
     * @return 结果
     */
    @Override
    public int deleteCompanyByIds(Long[] ids)
    {
        return companyMapper.deleteCompanyByIds(ids);
    }

    /**
     * 删除公司信息
     *
     * @param id 公司主键
     * @return 结果
     */
    @Override
    public int deleteCompanyById(Long id)
    {
        return companyMapper.deleteCompanyById(id);
    }
}
